package com.haoting.mamba.news.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haoting.mamba.news.entity.SupportEntity;
import com.haoting.mamba.news.entity.vo.SupportVo;
import com.haoting.mamba.news.service.SupportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.haoting.mamba.news.entity.NewsEntity;
import com.haoting.mamba.news.service.NewsService;
import com.haoting.common.utils.PageUtils;
import com.haoting.common.utils.R;



/**
 * 
 *
 * @author tanghao
 * @email 767417609@qq.com
 * @date 2021-03-14 19:47:11
 */
@RestController
@RequestMapping("news/news")
public class NewsController {
    @Autowired
    private NewsService newsService;

    @Autowired
    private SupportService supportService;
    /**
     * 列表
     */
    @GetMapping("/list")
//    @RequiresPermissions("news:news:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = newsService.queryPage(params);

        return R.ok().put("page", page);
    }

    @GetMapping("/listall")
    public R listall(@RequestParam Map<String, Object> params){
        PageUtils page = newsService.queryPageall(params);

        return R.ok().put("page", page);
    }

    @GetMapping("/gettodayNews")
//    @RequiresPermissions("news:news:list")
    public R gettodayNews() throws ParseException {
        Date date = new Date();
//        Calendar cal=Calendar.getInstance();
//        cal.add(Calendar.DATE,-1);
//        Date d=cal.getTime();

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date d = formatter.parse("2021-04-24");
        QueryWrapper<NewsEntity> wrapper = new QueryWrapper<>();
        wrapper.like("created",formatter.format(d))
        .or()
        .like("created",formatter.format(date));
        List<NewsEntity> newsEntities = newsService.list(wrapper);

        return R.ok().put("news", newsEntities);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
//    @RequiresPermissions("news:news:info")
    public R info(@PathVariable("id") String id){
		NewsEntity news = newsService.getById(id);
        return R.ok().put("news",news);
    }

//    @GetMapping("/dianzan")
//    public R dianzan(){
//
//    }
    /**
     * 保存
     */
    @PostMapping("/save")
//    @RequiresPermissions("news:news:save")
    public R save(@RequestBody NewsEntity news){
		newsService.save(news);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
//    @RequiresPermissions("news:news:update")
    public R update(@RequestBody NewsEntity news){
		newsService.updateById(news);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
//    @RequiresPermissions("news:news:delete")
    public R delete(@RequestBody String[]  ids){
		newsService.removeByIds(Arrays.asList( ids));

        return R.ok();
    }

    @GetMapping("/addlook/{id}")
    public R addlook(@PathVariable("id") String id){
        NewsEntity one = newsService.getOne(new QueryWrapper<NewsEntity>().eq("id", id));
//        one.setCollectionCount(one.getCollectionCount()+1);
//        newsService.updateById(one);
        return R.ok().put("dianzan",one.getCollectionCount());
    }

    @GetMapping("/addlooknum/{id}/{num}")
    public R addlooknum(@PathVariable("id") String id,@PathVariable("num") int num){
        NewsEntity one = newsService.getOne(new QueryWrapper<NewsEntity>().eq("id", id));
        System.out.println(num);
        one.setCollectionCount(num);
        newsService.updateById(one);
        return R.ok().put("one",one);
    }


    @PostMapping("/addsupport")
    public R addsupport(@RequestBody SupportEntity supportEntity){

       supportService.save(supportEntity);
       return R.ok();
    }

    @PostMapping("/delsupport")
    public R delsupport(@RequestBody SupportEntity supportEntity){
         QueryWrapper<SupportEntity> wrapper = new QueryWrapper<>();
         wrapper.eq("news_id",supportEntity.getNewsId())
                .and(wrapper1 -> wrapper1.eq("user_id",supportEntity.getUserId()));

        supportService.remove(wrapper);
        return R.ok();
    }

    @PostMapping("/supportor")
    public R supportor(@RequestBody SupportEntity supportEntity){
        QueryWrapper<SupportEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("news_id",supportEntity.getNewsId())
                .and(wrapper1 -> wrapper1.eq("user_id",supportEntity.getUserId()));
        int count = supportService.count(wrapper);
        if(count>0){
            return R.ok().put("result",0);
        }else {
            return R.ok().put("result",1);
        }

    }

    @PostMapping("/updatesupportsum/{newsId}")
    public R updatesupportsum(@PathVariable("newsId") String newsId){
            newsService.updatesupportsum(newsId);
            return R.ok();
    }
}
